package com.fivetalks.client;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.fivetalks.object.Member;
import com.fivetalks.object.RoomInfo;

public class ChatLobbyActivity extends Activity implements Runnable,
		OnClickListener {

	ArrayList<RoomInfo> roomList;
	ArrayList<Long> readyRoomNumber;
	Thread m_thread;
	public static Member member;
	CustomAdapter mAdapter;
	ListView list;
	String title = null;
	int count = 0;

	ImageButton btMakeRoom;
	ImageButton btGoReadyRoom;
	ImageButton btShowReadyRoom;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chat_lobby);

		Intent intent = getIntent();
		member = (Member) intent.getSerializableExtra("member");

		list = (ListView) findViewById(R.id.list);
		roomList = new ArrayList<RoomInfo>();
		readyRoomNumber = new ArrayList<Long>();
		mAdapter = new CustomAdapter(this, R.layout.list_view_row, roomList,
				member);
		list.setAdapter(mAdapter);

		btMakeRoom = (ImageButton) findViewById(R.id.btMakeRoom);
		btGoReadyRoom = (ImageButton) findViewById(R.id.btGoReadyRoom);
		btShowReadyRoom = (ImageButton) findViewById(R.id.btShowReadyRoom);
		btMakeRoom.setOnClickListener(this);
		btGoReadyRoom.setOnClickListener(this);
		btShowReadyRoom.setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		start();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	public void onClick(View v) {
		Intent intent;

		switch (v.getId()) {
		case R.id.btMakeRoom:
			intent = new Intent(this, RoomDialogActivity.class);
			startActivity(intent);
			break;

		case R.id.btGoReadyRoom:
			if (readyRoomNumber.size() == 0) {
				Toast.makeText(this, "방이 없습니다.", Toast.LENGTH_SHORT).show();
			} else {
				intent = new Intent(this, PreChatActivity.class);
				intent.putExtra("roomNumber", readyRoomNumber
						.get(getRandomNumber(readyRoomNumber.size())));
				startActivity(intent);
			}
			break;

		case R.id.btShowReadyRoom:
			count++;
		//	if (isReady())
		//		btShowReadyRoom.setText("모든 방 보기");
		//	else
		//		btShowReadyRoom.setText("입장 가능한 방 보기");
		//	onResume();
			break;
		}
	}

	public void start() {
		m_thread = new Thread(this);
		m_thread.start();
	}

	public void addResult(RoomInfo room) {
		roomList.add(room);
		refreshView();
	}

	public void refreshView() {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mAdapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	public void run() {
		SocketClient sc = SocketClient.getInstance(ChatLobbyActivity.this,null,null,null,null,null,null,null);
		// 로그인할때 다음과 같이 설정을 해야한다.
		sc.setAge(member.getAge());
		sc.setGender(member.getGender());
		sc.setNickName(member.getNickname());
		sc.setUserId(member.getId());
		sc.setEmail(member.getEmail());
		sc.setPassword(member.getPassword());
		try {
			sc.getDos().writeUTF("login");
			sc.getDos().writeUTF(sc.getUserId() + ""); // userId
			sc.getDos().writeUTF(sc.getNickName()); // nickName;
			sc.getDos().writeUTF(sc.getGender()); // MALE , FEMALE
			sc.getDos().writeUTF(sc.getAge() + ""); // age
			sc.getDos().writeUTF(sc.getEmail());
			sc.getDos().flush();

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		
	}

	public boolean isReady() {
		if (count % 2 != 0)
			return true;
		return false;
	}

	public int getRandomNumber(int size) {
		return (int) (Math.random() * size);
	}
}